﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonLibrary
{
    public class RecordLog
    {
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="msg"></param>
        public static void Write(string msg)
        {
            try
            {
                string appDomainPath = AppDomain.CurrentDomain.BaseDirectory;

                string logDiretoryPath = appDomainPath + "log";

                string logFileName = DateTime.Now.ToString("yyyyMMdd") + ".log";

                string logFilePath = logDiretoryPath + "\\" + logFileName;

                if (!Directory.Exists(logDiretoryPath))
                    Directory.CreateDirectory(logDiretoryPath);

                if (!File.Exists(logFilePath))
                {
                    FileStream fs1 = File.Create(logFilePath);
                    fs1.Close();
                }

                using (FileStream fs = new FileStream(logFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default))
                    {
                        sw.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]");
                        sw.WriteLine(msg);
                    }
                }
            }
            catch (Exception ex)
            {
                //using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))
                //{
                //    sw.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "]");
                //    sw.WriteLine(ex.Message);
                //}
            }
        }

        public static void Write(Exception ex)
        {
            try
            {
                string appDomainPath = AppDomain.CurrentDomain.BaseDirectory;

                string logDiretoryPath = appDomainPath + "\\log";

                string logFileName = DateTime.Now.ToString("yyyyMMdd") + ".log";

                string logFilePath = logDiretoryPath + "\\" + logFileName;

                if (!Directory.Exists(logDiretoryPath))
                    Directory.CreateDirectory(logDiretoryPath);

                if (!File.Exists(logFilePath))
                {
                    FileStream fs1 = File.Create(logFilePath);
                    fs1.Close();
                }

                using (FileStream fs = new FileStream(logFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default))
                    {
                        sw.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]");
                        sw.WriteLine(ex.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                //using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))
                //{
                //    sw.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "]");
                //    sw.WriteLine(ex.Message);
                //}
            }
        }
    }
}
